﻿using JNPF.Basics.Models.Common.Model;
using JNPF.Utils;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic.Common.Login
{
    /// <summary>
    /// 登录权限
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.07
    /// </summary>
    public class LoginBll
    {
        private LoginIService service = new LoginService();
        /// <summary>
        /// 获取权限（菜单、按钮、列表）
        /// </summary>
        /// <param name="isCache">默认缓存开启</param>
        /// <returns></returns>
        public async Task<LoginCurrentUserModel> GetAuthorize(bool isCache = true)
        {
            try
            {
                var userInfo = UserProvider.Instance.Get();
                var loginCurrentUserModel = new LoginCurrentUserModel();
                if (isCache == true)
                {
                    var cacheKey = CacheKey.USERAUTHORIZE + userInfo.UserId;
                    if (!CacheFactory.Cache.Exists(cacheKey))
                    {
                        loginCurrentUserModel = await service.GetAuthorize(userInfo.IsAdministrator, userInfo.UserId);
                    }
                    else
                    {
                        loginCurrentUserModel = CacheFactory.Cache.Get<LoginCurrentUserModel>(cacheKey);
                    }
                }
                else
                {
                    loginCurrentUserModel = await service.GetAuthorize(userInfo.IsAdministrator, userInfo.UserId);
                }
                return loginCurrentUserModel;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
